<template>
  <div class="wrapper-list">
    <!-- 条件区 -->
    <div class="wrapper-condition">
      <el-form :inline="true" label-position="left" label-width="80px" size="small">
        <el-form-item>
          <el-button type="success" icon="Plus" @click="onAdd">添加新类别</el-button>
        </el-form-item>
        <el-form-item label="所属类别">
          <el-radio-group v-model="tagLabel" @change="onTagChange">
            <el-radio v-for="item in allCategoryTagList" :key="item.id" :value="item.label" :label="item.label" />
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据列表区 -->
    <div class="wrapper-table">
      <el-table
        v-loading="flagLoading"
        :data="tblDataList"
        :tree-props="{ children: 'children' }"
        :header-cell-style="{ 'text-align': 'center' }"
        row-key="id"
        default-expand-all
        fit
        size="small"
        highlight-current-row
        style="width: 100%"
        height="100%"
      >
        <el-table-column prop="label" label="分类名称" width="400px" />

        <el-table-column prop="tag" align="center" label="标签" width="100px" />

        <el-table-column prop="id" align="center" label="编号" width="80px" />

        <el-table-column prop="depth" align="center" label="级别" width="80px" />

        <el-table-column prop="weight" align="center" label="排序" width="80px" />

        <el-table-column prop="urlCoverImg" align="center" label="封面图" />

        <el-table-column align="center" label="操作" width="120">
          <template #default="scope">
            <div class="wrapper-op">
              <a class="ibtn ibtn-edit" title="修改" @click="onEdit(scope.row)">
                <el-icon><Edit /></el-icon>
              </a>
              <a class="ibtn ibtn-del" title="删除" @click="onDelete(scope.row.id)">
                <el-icon><Delete /></el-icon>
              </a>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { AryToTree } from '@/utils/array'
import { toastSuccess } from '@/utils/toast'
import { getLStorage, setSStorageObj } from '@/utils/web-storage'

import { ElMessageBox } from 'element-plus'
import router from '@/router'
import store from '@/store'
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'CategoryListAllView',
  setup() {
    const allCategoryTagList = ref([])
    const tagLabel = ref('')
    const tblDataList = ref([])
    const flagLoading = ref(true)

    const initAllCategoryTagList = () => {
      allCategoryTagList.value = JSON.parse(getLStorage('AllCategoryTag'))
      tagLabel.value = allCategoryTagList.value[0].label
      listAll(tagLabel.value)
    }

    const onTagChange = (e) => {
      listAll(e)
    }
    const listAll = (tag) => {
      store
        .dispatch('sys_category/listAll', tag)
        .then((response) => {
          flagLoading.value = false
          tblDataList.value = AryToTree(response, 0)
        })
        .catch((error) => {
          flagLoading.value = false
          console.log('得到所有的分类出现错误：', error)
        })
    }
    const onAdd = () => {
      router.push({ path: 'add' })
    }
    const onEdit = (row) => {
      if (row.id === 1) return
      setSStorageObj('ParamData', row)
      router.push({ path: 'edit' })
    }
    const onDelete = (id) => {
      if (id === 1) return
      ElMessageBox.confirm('确定要删除该类别吗？', '删除警告', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          store
            .dispatch('sys_category/delById', id)
            .then(() => {
              toastSuccess('删除成功！')
              listAll(tagLabel.value)
            })
            .catch((error) => {
              console.log('删除分类出现错误：', error)
            })
        })
        .catch(() => {})
    }

    onMounted(() => {
      initAllCategoryTagList()
    })

    return {
      allCategoryTagList,
      tagLabel,
      tblDataList,
      flagLoading,
      onTagChange,
      initAllCategoryTagList,
      listAll,
      onAdd,
      onEdit,
      onDelete,
    }
  },
})
</script>

<style lang="scss" scoped></style>
